package com.ljq.cksp.service.impl;

import com.ljq.cksp.dao.BookDao;
import com.ljq.cksp.pojo.Book;
import com.ljq.cksp.pojo.vo.BookVO;
import com.ljq.cksp.service.BookService;
import com.ljq.cksp.util.JsonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:ljq
 * @Date:2020/4/22
 */
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    BookDao bookDao;

    @Override
    public List<BookVO> query(String keyword) {
        List<BookVO> bookVOS = new ArrayList<>();
        List<Book>books = bookDao.findByTitleContains(keyword);
        for(Book book:books){
            BookVO bookVO = new BookVO();
            BeanUtils.copyProperties(book, bookVO);
            bookVOS.add(bookVO);
        }
        return bookVOS;
    }

    @Override
    public Book get(int id) {
        return bookDao.findById(id);
    }

    @Override
    public void saveFromFile(File file) {
        Book book = JsonUtil.json2Book(file);
        bookDao.save(book);
    }
}
